import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

import Layout from '@/layout'

// 静态路由
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/register',
    name: 'Register',
    component: () => import('@/views/register/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: '物业系统',
      component: () => import('@/views/dashboard/index'),
      meta: { title: '首页', icon: 'dashboard' }
    }]
  }
]

// 动态路由
export const asyncRoutes = [
  {
    path: '/user',
    component: Layout,
    redirect: '/',
    children: [{
      path: '',
      name: 'User',
      component: () => import('@/views/user/index'),
      meta: { title: '用户管理', icon: 'el-icon-user', roles: ['superadmin'] }
    }]
  },

  {
    path: '/community',
    component: Layout,
    redirect: '/community/list',
    name: 'Community',
    meta: { title: '小区管理', icon: 'el-icon-office-building', roles: ['superadmin', 'admin'] },
    children: [
      {
        path: 'list',
        name: 'communityList',
        component: () => import('@/views/community/list'),
        meta: { title: '全部小区', icon: 'table', roles: ['superadmin', 'admin'] }
      },
      {
        path: 'add',
        name: 'CommunityAdd',
        component: () => import('@/views/community/add'),
        meta: { title: '添加小区', icon: 'form', roles: ['superadmin', 'admin'] }
      }
    ]
  },

  {
    path: '/building',
    component: Layout,
    redirect: '/building/list',
    name: 'Building',
    meta: { title: '楼栋管理', icon: 'el-icon-s-home', roles: ['superadmin', 'admin'] },
    children: [
      {
        path: 'list',
        name: 'BuildingList',
        component: () => import('@/views/building/list'),
        meta: { title: '全部楼栋', icon: 'table', roles: ['superadmin', 'admin'] }
      },
      {
        path: 'add',
        name: 'BuildingAdd',
        component: () => import('@/views/building/add'),
        meta: { title: '添加楼栋', icon: 'form', roles: ['superadmin', 'admin'] }
      }
    ]
  },

  {
    path: '/owner',
    component: Layout,
    redirect: '/owner/list',
    name: 'Owner',
    meta: { title: '业主管理', icon: 'el-icon-s-custom', roles: ['superadmin', 'admin'] },
    children: [
      {
        path: 'list',
        name: 'OwnerList',
        component: () => import('@/views/owner/list'),
        meta: { title: '全部业主', icon: 'table', roles: ['superadmin', 'admin'] }
      },
      {
        path: 'add',
        name: 'OwnerAdd',
        component: () => import('@/views/owner/add'),
        meta: { title: '添加业主', icon: 'form', roles: ['superadmin', 'admin'] }
      }
    ]
  },

  {
    path: '/vehicle',
    component: Layout,
    redirect: '/vehicle/list',
    name: 'Vehicle',
    meta: { title: '车辆管理', icon: 'el-icon-bicycle', roles: ['superadmin', 'admin'] },
    children: [
      {
        path: 'list',
        name: 'VehicleList',
        component: () => import('@/views/vehicle/list'),
        meta: { title: '全部车辆', icon: 'table', roles: ['superadmin', 'admin'] }
      },
      {
        path: 'add',
        name: 'VehicleAdd',
        component: () => import('@/views/vehicle/add'),
        meta: { title: '添加车辆', icon: 'form', roles: ['superadmin', 'admin'] }
      }
    ]
  },

  {
    path: '/parking',
    component: Layout,
    redirect: '/parking/list',
    name: 'Parking',
    meta: { title: '停车位管理', icon: 'el-icon-place', roles: ['superadmin', 'admin'] },
    children: [
      {
        path: 'list',
        name: 'ParkingList',
        component: () => import('@/views/parking/list'),
        meta: { title: '全部车位', icon: 'table', roles: ['superadmin', 'admin'] }
      },
      {
        path: 'add',
        name: 'ParkingAdd',
        component: () => import('@/views/parking/add'),
        meta: { title: '添加车位', icon: 'form', roles: ['superadmin', 'admin'] }
      }
    ]
  },

  {
    path: '/payment',
    component: Layout,
    redirect: '/payment/list',
    name: 'Payment',
    meta: { title: '缴费管理', icon: 'el-icon-coin', roles: ['superadmin', 'admin'] },
    children: [
      {
        path: 'list',
        name: 'PaymentList',
        component: () => import('@/views/payment/list'),
        meta: { title: '全部缴费', icon: 'table', roles: ['superadmin', 'admin'] }
      },
      {
        path: 'add',
        name: 'PaymentAdd',
        component: () => import('@/views/payment/add'),
        meta: { title: '添加缴费', icon: 'form', roles: ['superadmin', 'admin'] }
      }
    ]
  },

  {
    path: '/repair',
    component: Layout,
    redirect: '/repair/list',
    name: 'Repair',
    meta: { title: '报修管理', icon: 'el-icon-brush', roles: ['superadmin', 'admin', 'owner'] },
    children: [
      {
        path: 'list',
        name: 'RepairList',
        component: () => import('@/views/repair/list'),
        meta: { title: '全部报修', icon: 'table', roles: ['superadmin', 'admin'] }
      },
      {
        path: 'add',
        name: 'RepairAdd',
        component: () => import('@/views/repair/add'),
        meta: { title: '添加报修', icon: 'form', roles: ['superadmin', 'admin', 'owner'] }
      }
    ]
  },

  {
    path: '/complaint',
    component: Layout,
    redirect: '/complaint/list',
    name: 'Complaint',
    meta: { title: '投诉管理', icon: 'el-icon-chat-line-round', roles: ['superadmin', 'admin', 'owner'] },
    children: [
      {
        path: 'list',
        name: 'ComplaintList',
        component: () => import('@/views/complaint/list'),
        meta: { title: '全部投诉', icon: 'table', roles: ['superadmin', 'admin'] }
      },
      {
        path: 'add',
        name: 'ComplaintAdd',
        component: () => import('@/views/complaint/add'),
        meta: { title: '添加投诉', icon: 'form', roles: ['superadmin', 'admin', 'owner'] }
      }
    ]
  },

  {
    path: '/mailbox',
    component: Layout,
    redirect: '/mailbox/list',
    name: 'Mailbox',
    meta: { title: '信箱管理', icon: 'el-icon-message', roles: ['superadmin', 'admin', 'owner'] },
    children: [
      {
        path: 'list',
        name: 'MailboxList',
        component: () => import('@/views/mailbox/list'),
        meta: { title: '全部信箱', icon: 'table', roles: ['superadmin', 'admin'] }
      },
      {
        path: 'add',
        name: 'MailboxAdd',
        component: () => import('@/views/mailbox/add'),
        meta: { title: '添加信箱', icon: 'form', roles: ['superadmin', 'admin', 'owner'] }
      }
    ]
  },
  // 404 page must be placed at the end!!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
